package com.dhec.ptms.epr.db;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhec.ptms.epr.model.ReferBenchMarkDetail;
import com.ionglobal.db.DBI;
import com.ionglobal.lib.SysException;

public class ReferBenchMarkDetailDB {
	
	private static final Logger logger = LoggerFactory.getLogger(ReferBenchMarkDetailDB.class);

	public List<ReferBenchMarkDetail> list(DBI dbi, int benchMarkId, int page, int pagesize){
		ArrayList<ReferBenchMarkDetail> list = new ArrayList<ReferBenchMarkDetail>(0);
		try {
			dbi.skipToPage(pagesize, page, "select * from EPR_ReferBenchMarkDetail where benchMarkId="+benchMarkId+" order by id asc");
			while(dbi.rsNext()){
				ReferBenchMarkDetail bean = new ReferBenchMarkDetail();
				this.handleData(dbi, bean);
				list.add(bean);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("ReferBenchMarkDetailDB.list(DBI, int, int, int) Error!", e);
		}
		
		return list;
		
	}
	
	public long count(DBI dbi, int benchMarkId){
		long c = 0;
		try {
			dbi.execQuery("select count(*) from EPR_ReferBenchMarkDetail where benchMarkId="+benchMarkId);
			if(dbi.rsNext()){
				c = dbi.rsGetLong(1);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("ReferBenchMarkDetailDB.list(DBI) Error!", e);
		}
		
		return c;
		
	}
	
	public void handleData(DBI dbi, ReferBenchMarkDetail bean) throws SysException{
		bean.setId(dbi.rsGetInt("id"));
		bean.setKw(new BigDecimal(dbi.rsGetString("kw")));
		bean.setNm(new BigDecimal(dbi.rsGetString("nm")));
		bean.setRpm(dbi.rsGetInt("rpm"));
		bean.setBenchMarkId(dbi.rsGetInt("benchMarkId"));
	}
	
	
	public ReferBenchMarkDetail find(DBI dbi, int id){
		ReferBenchMarkDetail bean = null;
		try {
			dbi.execQuery("select * from EPR_ReferBenchMarkDetail where "
					+ "id="+id);
			if(dbi.rsNext()){
				bean = new ReferBenchMarkDetail();
				this.handleData(dbi, bean);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("ReferBenchMarkDetailDB.find(DBI, int) Error!", e);
		}
		
		return bean;
		
	}
	
	public boolean update(DBI dbi, ReferBenchMarkDetail bean){
		try {
			return dbi.execute("update EPR_ReferBenchMarkDetail set kw="+bean.getKw()+","
					+ "benchMarkId="+bean.getBenchMarkId()+","
					+ "nm="+bean.getNm()+","
					+ "rpm="+bean.getRpm()
					+ " where id="+bean.getId());
		} catch (SysException e) {
			logger.error("ReferBenchMarkDetailDB.update(DBI, ReferBenchMarkDetail) Error!", e);
		}
		
		return false;
		
	}
	
	public boolean save(DBI dbi, ReferBenchMarkDetail bean){
		try {
			return dbi.execute("insert into EPR_ReferBenchMarkDetail (id, benchMarkId, kw, nm, rpm) values("+bean.getId()+","
					+ bean.getBenchMarkId()+","
					+ bean.getKw()+","
					+ bean.getNm()+","
					+ bean.getRpm()+")");
		} catch (SysException e) {
			logger.error("ReferBenchMarkDetailDB.save(DBI, ReferBenchMarkDetail) Error!", e);
		}
		
		return false;
		
	}
	
	public boolean delete(DBI dbi, int benchMarkId){
		try {
			return dbi.execute("delete from EPR_ReferBenchMarkDetail where benchMarkId="+benchMarkId);
		} catch (SysException e) {
			logger.error("ReferBenchMarkDetailDB.delete(DBI, int) Error!", e);
		}
		
		return false;
		
	}
}
